//
//  ZXLAPISecurityManager.m
//  SignSecurityDemo
//
//  Created by awh on 2020/8/23.
//  Copyright © 2020 CordovaDemo. All rights reserved.
//

#import "ZXLAPISecurityManager.h"
#import "ZXLSignTools.h"
#import "ZXLSecurityClient.h"
@implementation ZXLAPISecurityManager
-(SignResult*)getAPIServiceResultWith:(ZXLSignData*)signEntity  config:(ZXLConfig*)zxlApiConfig{
    SignResult * result =[SignResult new];
    if (!signEntity.serviceType) {
        result.msg=@"请设置serviceType";   return result;
    }
    switch (signEntity.serviceType) {
        case SecuritySceneServiceTypeEncrypt:
            //加密
            if (!signEntity.StringToSign) {
                result.msg=@"StringToSign不能为空";   return result;
            }
            result =  [ZXLSignTools encryptType:signEntity.encryptType StringToSign:signEntity.StringToSign zxlConfig:zxlApiConfig];
            break;
        case SecuritySceneServiceTypeDecrypt:
            //解密
              result =[ZXLSignTools decryptionType:signEntity.encryptType decryptionStr:signEntity.decryptionStr zxlConfig:zxlApiConfig];
            break;
        case SecuritySceneServiceTypeSignal:
        {
            //加签
            NSData * data = [ZXLSignToolRSA sha256WithRSA:[NSData data] withPrivatKey:zxlApiConfig.signatureKey];
            result.signResultStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        }
            break;
        case SecuritySceneServiceTypeVertify:
            //验签
        {
            NSData * data  = [ZXLSignToolRSA sha256WithRSA:[NSData data] withPrivatKey:zxlApiConfig.signatureKey];
            BOOL  Vertify  = [ZXLSignToolRSA rsaSHA256VertifyingData:[NSData data] withSignature:data withpublicKey:zxlApiConfig.vertifyKey];
            if (Vertify) {
                result.msg=@"验签成功";
            }else{
                result.msg=@"验签失败";
            }
        }
            break;
        default:
            break;
    }
    return result;
}




//
//-(NSString*)getiStringToSign:(ZXLSignEntity*)signEntity{
//    NSString *  str = [NSString stringWithFormat:@"%@+%@+%@",signEntity.queryString,signEntity.body,signEntity.nonce];
//    return str;
//}
/*       v2
//
//-(SignResult*)getEncryptWith:(ZXLSignEntity*)signEntity  config:(ZXLConfig*)zxlApiConfig{
//     SignResult * result =[SignResult new];
//     signEntity.StringToSign = [self getiStringToSign:signEntity];
//
//     result =  [ZXLSignTools encryptType:signEntity.encryptType StringToSign:signEntity.StringToSign zxlConfig:zxlApiConfig];
//     return result;
//}

-(SignResult*)getDecryptionWith:(ZXLSignEntity*)signEntity  config:(ZXLConfig*)zxlApiConfig{
     SignResult * result =[SignResult new];
     result =[ZXLSignTools decryptionType:signEntity.encryptType decryptionStr:signEntity.decryptionStr zxlConfig:zxlApiConfig];
     return result;
}

-(SignResult*)getSignWith:(ZXLSignEntity*)signEntity  config:(ZXLConfig*)zxlApiConfig{
    SignResult * result =[SignResult new];
    if (zxlApiConfig.isEncrypt== SecuritySignSignal) {
        NSData * data = [ZXLSignToolRSA sha256WithRSA:[NSData data] withPrivatKey:zxlApiConfig.signatureKey];
        result.signResultStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
       }else if (zxlApiConfig.isEncrypt==SecuritySignVertify){
           NSData * data   = [ZXLSignToolRSA sha256WithRSA:[NSData data] withPrivatKey:zxlApiConfig.signatureKey];
           BOOL  Vertify  = [ZXLSignToolRSA rsaSHA256VertifyingData:[NSData data] withSignature:data withpublicKey:zxlApiConfig.vertifyKey];
           if (Vertify) {
               result.msg=@"验签成功";
           }else{
               result.msg=@"验签失败";
           }
       }
    NSLog(@"result: %@",result.msg);
    return result;
}

-(SignResult*)getVerifySignWith:(ZXLSignEntity*)signEntity  config:(ZXLConfig*)zxlApiConfig{
    SignResult * result =[SignResult new];
    zxlApiConfig.isEncrypt=SecuritySignVertify;
    result = [self getSignWith:signEntity config:zxlApiConfig];
    return result;
}  */

/*
-(SignResult*)getSignWith:(ZXLAPIConfig*)zxlApiConfig{
    SignResult * result =[SignResult new];
    if (!zxlApiConfig.signType) {
        result.msg=@"请设置signType";   return result;
    }
    switch (zxlApiConfig.signType) {
        case SecuritySignTypeMD5:
            if (zxlApiConfig.md5Bit==SecuritySignMD5bitType16bit) {
                 result .signResultStr =  [ZXLSignTools getMd5_32Bit_String:zxlApiConfig.signText isUppercase:zxlApiConfig.isUppercase];
                }else{
                result .signResultStr = [ZXLSignTools getMd5_32Bit_String:zxlApiConfig.signText isUppercase:zxlApiConfig.isUppercase];
            }
            break;
        case SecuritySignTypeSHA:
            result .signResultStr= [ZXLSignTools SHAEncryptWithString:zxlApiConfig.signText withType:zxlApiConfig.SHAEncryType withLowercase:zxlApiConfig.isLowercase];
            break;
        case SecuritySignTypeAES:
            if (zxlApiConfig.isEncrypt==SecuritySignEncrypt) {
                result .signResultStr=[ZXLSignToolAES AESEncrypt:zxlApiConfig.signText key:zxlApiConfig.appKey keySize:zxlApiConfig.AESLengthType];
            }else if(zxlApiConfig.isEncrypt==SecuritySignDecrypt){
                result .signResultStr=[ZXLSignToolAES AESDecrypt:zxlApiConfig.signText key:zxlApiConfig.appKey keySize:zxlApiConfig.AESLengthType];
            }
            break;
        case SecuritySignTypeDES:
            if (zxlApiConfig.isEncrypt==SecuritySignEncrypt) {
                result .signResultStr= [ZXLSignToolDES encryptUseDES:zxlApiConfig.signText key:zxlApiConfig.appKey];
            }else if(zxlApiConfig.isEncrypt==SecuritySignDecrypt){
                result .signResultStr= [ZXLSignToolDES decryptUseDES:zxlApiConfig.signText key:zxlApiConfig.appKey];
            }
            break;
        case SecuritySignTypeRSA:
            if (zxlApiConfig.isEncrypt==SecuritySignEncrypt) {
                result .signResultStr=  [ZXLSignToolRSA encryptString:zxlApiConfig.signText publicKey:zxlApiConfig.pubKey];
            }else if(zxlApiConfig.isEncrypt==SecuritySignDecrypt){
                result .signResultStr=  [ZXLSignToolRSA decryptString:zxlApiConfig.signText privateKey:zxlApiConfig.priKey];
            }else{
                result= [self rsaSignalWithVertify:zxlApiConfig];
            }
            break;
        default:
            break;
    }
    return result;
}

-(SignResult*)rsaSignalWithVertify :(ZXLAPIConfig*)config{
    SignResult * result =[SignResult new];
    if (config.isEncrypt== SecuritySignSignal) {
    //加签名
        result. signResultData  =  [ZXLSignToolRSA sha256WithRSA:config.signData withPrivatKey:config.priKey];
    }else if (config.isEncrypt==SecuritySignVertify){
        NSData * data   = [ZXLSignToolRSA sha256WithRSA:config.signData withPrivatKey:config.priKey];
        BOOL  Vertify  = [ZXLSignToolRSA rsaSHA256VertifyingData:config.signData withSignature:data withpublicKey:config.pubKey];
        if (Vertify) {
            result.msg=@"验签成功";
            NSLog(@"验签成功");
        }else{
            result.successCode=NO;
            result.msg=@"验签失败";
        }
    }
    return result;
}*/
@end
